package tech.aistar.day09;

import tech.aistar.day09.homework.Purcase;

import java.util.Comparator;

/**
 * 本类用来演示: 比较器工厂
 *
 * @author: success
 * @date: 2022/7/29 11:04 上午
 */
public class ComparatorFactory {

    public static final int PRICE = 0;
    public static final int BRAND = 1;
    public static final int XX = 2;

    public static Comparator<Purcase> getInstance(int type){
        Comparator<Purcase> c = null;

        switch (type){
            case PRICE:
                c = (p1,p2)-> (int) (p2.getCost() - p1.getCost());
                break;
            case BRAND:
                c = (p1,p2)-> p2.getBrand().compareTo(p1.getBrand());
                break;
            case XX:
                //根据price价格降序,如果price一致,继续根据brand降序
                c = (p1,p2)->{
                    if(p2.getCost()>p1.getCost()){
                        return 1;
                    }else if(p2.getCost()<p1.getCost()){
                        return -1;
                    }else{
                        return p2.getBrand().compareTo(p1.getBrand());
                    }
                };
                break;
        }

        return c;
    }

}
